//
//  YCInputVerifyCodeView.m
//  HeartWorldClient
//
//  Created by 董知樾 on 2017/7/14.
//  Copyright © 2017年 谭明权. All rights reserved.
//

#import "YCInputVerifyCodeView.h"
#import "YCTextField.h"

@interface YCInputVerifyCodeView ()<UITextFieldDelegate, YCTextFieldDeleteBackDelegate>

@property (strong, nonatomic) IBOutletCollection(YCTextField) NSArray<YCTextField *> *codeTextFields;
@property (strong, nonatomic) IBOutletCollection (UIView) NSArray*singleCodeLineViews;
@property (copy, nonatomic) void (^inputFinishBlock)(NSString *code);
@property (weak, nonatomic) IBOutlet UIView *coverView;

@end

@implementation YCInputVerifyCodeView


- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self initializeConfig];
}

//初始设置
- (void)initializeConfig {
    for (NSInteger i = 0; i < _codeTextFields.count; i ++) {
        YCTextField *textField = _codeTextFields[i];
        [textField addTarget:self action:@selector(inputCode:) forControlEvents:UIControlEventEditingChanged];
        textField.text = nil;
        textField.tintColor = [UIColor colorWithRGBValue:0x333333];
        textField.deleteBackDelegate = self;
        if (i == 0) {
            [textField becomeFirstResponder];
        }
    }
    
    [_coverView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(inputCodeAgain)]];
    _coverView.hidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(autoInputCode:) name:@"verify_code_noti" object:nil];
}

//MARK:~~~~ code input ~~~~

- (void)autoInputCode:(NSNotification *)noti {
    NSString *code = (NSString *)noti.object;
    for (NSInteger i = 0; i < code.length; i ++) {
        _codeTextFields[i].text = [code substringWithRange:NSMakeRange(i, 1)];
    }
    if (_inputFinishBlock && code.length == 4) {
        [_codeTextFields.lastObject resignFirstResponder];
        _inputFinishBlock(code);
    }
}

- (void)inputCode:(YCTextField *)textField {
    if (textField.text.length >= 1) {
        
        NSInteger index = [_codeTextFields indexOfObject:textField];
        if (index == _codeTextFields.count - 1) {//输入完毕
        } else {
            
            UITextField *next = _codeTextFields[index + 1];
            [next becomeFirstResponder];
        }
        
        NSMutableString *code = [NSMutableString string];
        for (UITextField *textF in _codeTextFields) {
            [code appendString:textF.text];
        }
        if (_inputFinishBlock && code.length == 4) {
            [textField resignFirstResponder];
            _inputFinishBlock(code);
        }
    }
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSInteger index = [_codeTextFields indexOfObject:textField];
    UIView *line = _singleCodeLineViews[index];
    line.backgroundColor = [UIColor colorWithCommonType:commonColorBlue];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSInteger index = [_codeTextFields indexOfObject:textField];
    UIView *line = _singleCodeLineViews[index];
    line.backgroundColor = [UIColor colorWithRGBValue:0xe7e7e7];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length + string.length > 1) {
        return NO;
    }
    if (string.length <= 1) {
        return YES;
    }
    
    return NO;
}

- (void)textFieldWillDeleteBackWord:(YCTextField *)textField {
    if (textField.text.length == 0) {
        NSInteger index = [_codeTextFields indexOfObject:textField];
        if (index <= 0) {
            return;
        }
        [textField resignFirstResponder];
        UITextField *last = _codeTextFields[index - 1];
        last.text = nil;
        [last becomeFirstResponder];
    }
}

- (void)inputCodeAgain {
    for (NSInteger i = 0; i < _codeTextFields.count; i ++) {
        UITextField *textField = _codeTextFields[i];
        textField.text = nil;
        if (i == 0) {
            [textField becomeFirstResponder];
        }
    }
}

- (void)inputFinish:(void (^)(NSString *))complete {
    _inputFinishBlock = complete;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"verify_code_noti" object:nil];
}

@end
